/*
 * Copyright © 2024-2025 Wenze Wei
 *
 * This file is part of StadionOS.
 *
 * This Source Code Form is subject to the terms of the GNU General Public License, v. 2.0.
 * You can obtain a copy of the GPL at
 * https://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 */

#include "../include/string.h"

/**
 * @brief Tokenize a string using a set of delimiters.
 *
 * @param str The string to tokenize. If NULL, the tokenizer will continue scanning the string from where it left off.
 * @param delim A string containing the delimiter characters.
 * @return A pointer to the next token, or NULL if no more tokens are found.
 */
char *Sstrtok(char *str, const char *delim) {
    static char *saved_str;
    if (str) saved_str = str;

    if (!saved_str) return NULL;

    while (*saved_str && strchr(delim, *saved_str)) saved_str++;  

    if (!*saved_str) return NULL;  

    char *token_start = saved_str;  

    while (*saved_str && !strchr(delim, *saved_str)) saved_str++;  

    if (*saved_str) {  
        *saved_str++ = '\0';  
    } else {  
        saved_str = NULL;
    }  

    return token_start;
}  